package com.kitty.game.pet.handler;

import com.kitty.game.pet.model.Pet;
import com.kitty.game.role.model.Role;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.config.PetStoneSet;
import com.kitty.game.item.service.PetStoneService;
import com.kitty.game.pet.PetDataPool;
import com.kitty.game.pet.bean.PetObject;
import com.kitty.game.pet.model.PetStone;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.List;

import static com.kitty.game.utils.Const.ROLE_RESOURCE_NOT_ENOUGH;

@Component("InsetStoneHandler")
@Slf4j
public class InsetStoneHandler extends FeedPetHandler {
    @Resource
    PetStoneService stoneService;
    private static final List<String> stoneNames = Arrays.asList("枯月流魂", "炫影霜星", "冰落残阳", "雷极弧光", "风寂云清", "凝香幻彩");
    private static final String ERROR_ITEM = "请放入正确的妖石。";
    private static final String REPEAT_STONE = "#Y{0}#n已打入{1}，不可重复打入！";
    private static final String FULL_STONE = "#Y{0}#n已经打入了#R3#n个妖石。";
    private static final String SUCCESS = "你成功为#Y{0}#n打入了#R{1}#n。";

    private String canFeed(Pet pet, RoleEquip roleEquip, PetObject petObject, PetStoneSet stoneSet) {
        /**打入妖石不能是野生 其他都可以*/
        if (isRightType(pet.getType())) {
            return MessageFormat.format(ERROR_PET_TYPE, pet.getName());
        }
//        if (true){
//            return "妖石功能维护中。。。";
//        }
        if (stoneSet == null) {
            return ERROR_ITEM;
        }
        if (!stoneNames.contains(roleEquip.getName())) {
            return ERROR_ITEM;
        }
        if (roleEquip.queryNum() < 1) {
            return MessageFormat.format(ROLE_RESOURCE_NOT_ENOUGH, roleEquip.getName());
        }
        if (petObject == null) {
            log.error("未找到宠物配置=={}", pet.getName());
            return MessageFormat.format(ERROR_PETSET, pet.getName());
        }
        if (pet.getStones().size() >= 3) {
            return MessageFormat.format(FULL_STONE, pet.getName());
        }
        if (pet.getStones() != null) {
            for (PetStone petStone : pet.getStones()) {
                if (petStone.getName().equals(stoneSet.getName())) {
                    return MessageFormat.format(REPEAT_STONE, pet.getName(), stoneSet.getName());
                }
            }
        }
        return null;
    }

    @Override
    public String acceptFeed(Pet pet, Role role, RoleEquip roleEquip) {
        PetObject petObject = PetDataPool.getPetObject(pet.getName());
        PetStoneSet stoneSet = stoneService.getStoneSetBy(roleEquip.getName(), roleEquip.queryLevel());
        String result = canFeed(pet, roleEquip, petObject, stoneSet);
        if (result != null) {
            return result;
        }
        equipService.delEquipNum(role, roleEquip, 1);
        PetStone petStone = new PetStone(stoneSet);
        pet.getStones().add(petStone);
        petService.refreshPetInfo(role, pet);

        return MessageFormat.format(SUCCESS, pet.getName(), stoneSet.getName());
    }
}
